//
// Description: 204. 表达整数的奇怪方式
// Created by Loading on 2022/5/28.
//

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

LL ex_gcd(LL a, LL b, LL &x, LL &y) {
    if (!b) {
        x = 1, y = 0;
        return a;
    }
    LL d = ex_gcd(b, a % b, y, x);
    y -= a / b * x;

    return d;
}

int main() {
    int n;
    cin >> n;

    LL a1, m1;
    cin >> a1 >> m1;
    // 是否有解
    bool has_answer = true;
    for (int i = 0; i < n - 1; ++i) {
        LL a2, m2;
        cin >> a2 >> m2;

        LL k1, k2;
        LL d = ex_gcd(a1, a2, k1, k2);
        // 有解的充要条件是，(m2 - m1) 是 d 的整数倍
        if ((m2 - m1) % d) {
            has_answer = false;
            break;
        }
        // 计算完成后要扩大 (m2 - m1) / d 倍
        k1 *=  (m2 - m1) / d;
        // 将 k1 变得足够小，否则可能会溢出
        LL t = a2 / d;
        k1 = (k1 % t + t) % t;

        m1 = a1 * k1 + m1;
        a1 = abs(a1 / d * a2);
    }
    if (has_answer) {
        cout << (m1 % a1 + a1) % a1 << endl;
    } else {
        cout << -1 << endl;
    }

    return 0;
}